package com.minelx.sqlstructures.statements.clause;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.joining;

public class SelectClause implements Sql {

	private static final String COMMA = ", ";

	private static final String ALL = "*";

	private final List<Field> fields;

	private final String tableName;

	private SelectClause(String tableName, List<Field> fields) {
		this.tableName = tableName;
		this.fields = fields;
	}

	@Override
	public String toText() {
		return "SELECT " +
				fields() +
				" FROM " +
				tableName;
	}

	List<Field> getFields() {
		return unmodifiableList(fields);
	}

	public String getTableName() {
		return tableName;
	}

	private String fields() {
		if (fields.isEmpty()) {
			return ALL;
		}
		return fields.stream().map(Field::toText).collect(joining(COMMA));
	}

	public static SelectClause of(String tableName, List<Field> fields) {
		return new SelectClause(tableName, fields);
	}
}
